<html>
<head>
<title> View team details</title>
<body>
<?php
if( isset( $_POST['team_id'] ) ) {

	PutEnv("ORACLE_HOME=/usr/local/dbpackages/oracle");

	$c = oci_connect('comp231stu032', 'chemistry', 'comp231.cse.ust.hk');

	if(!$c) {
		echo "<p>Unable to connect:" . var_dump(oci_error($c));
		die();
	}

	$s = "select * from form_1_team join staff on (form_1_team.staff_ID=staff.ID), 
		  			    form_2_team join staff on (form_2_team.driver_ID=staff.ID) 
					    where form_1_team.team_id=form_2_team.team_id and 
					          form_1_team.team_id=". $_POST['team_id'];


	$q = oci_parse($c, $s);


	oci_execute($q);
	//oci_fetch($q);
	
	echo "<h1>Welcome to the 231chefs Roster Generation System</h1>";
	echo "<h2>View Team</h2>";
	$error = true;
	while ($row2 = oci_fetch_array($q, OCI_BOTH)) {
		$tsl = (int)($row2[7]) | (int)($row2[16]);
		$arr = array(1, 2, 4, 8, 16);
		$arrskill = array ('Boeing 747', 'Boeing 757', 'Boeing 777', 'AirBus 340', 'AirBur 380');
		$skill = '';
		for($i = 0; $i < 5; $i++) {
			if($arr[$i] & $tsl) {
			$skill = $skill." ". $arrskill[$i];
			}
		}
		$wts = $row2[6];
		switch($wts) {
			case "MA":	$wts = $wts. " 05:00-14:00";break;
			case "AA":	$wts = $wts. " 12:00-21:00";break;
			case "EA":	$wts = $wts. " 15:00-24:00";break;
		}

		echo "<table border='1'>";
		echo "<tr><td>Team ID</td><td>" .$row2[1]. "</td></tr>";
		echo "<tr><td>Team Members</td><td>" .$row2[3]." ".$row2[4]."<br>".$row2[12]." ".$row2[13]. "</td></tr>";
		echo "<tr><td>Team Working time slot</td><td>$wts</td></tr>";
		echo "<tr><td>Team skill list</td><td>$skill</td></tr>";
		echo "<tr><td>Team reward per hour:</td><td>" .(floatval($row2[8])+floatval($row2[17]))."</td></tr>";
		echo "</table><br>";
		$error = false;
	}
	if ($error)
		echo "The Team ID does not exist.<br>";
	echo "<p>Click <a href='index.php'>here</a> to return to the Main Menu</p>";
	oci_close($c);

}
else {
?>
	<form method='POST'>
	Team ID: <input name='team_id' maxlength='3' />
	<button type='submit'>Show</button>
	</form>
<?php
}
?>
</body>
<html>

